#!/usr/bin/env python

import sys, os
import urllib

from banner_class import Banner

def init ( l, admin = 0 ):
	from liwe import parts

	Banner ( l )

	parts.add ( l.layman, [ "base", "ext" ] )

	l.layman.add_js ( "modules/banner2/js/banner.js" )
	l.layman.add_css ( "modules/banner2/css/style.css" )

	l.layman.add_js_code ( "banner2.init();" )

	if admin and int ( admin ):
		l.layman.add_js ( "modules/banner2/js/banner.admin.js" )
		l.layman.add_js ( "modules/banner2/js/month_selector.js" )
		l.layman.add_js ( "modules/stats/js/stats.js" )
		l.layman.add_css ( "modules/banner2/css/banner.admin.css" )
		l.layman.add_js_code ( "banner2.admin.init();" )
		parts.add_wwl ( l.layman, 'graph' )

	return ''

def load_banner ( liwe, id_tipo = 0, tipo = '' ):
	from os3.utils.utils import tmpnam

	b = Banner ( liwe )
	res = b.load_banner ( id_tipo = id_tipo, tipo = tipo )
	#sys.stderr.write ( "\n\n -----------------     --------------     load_banner : %s\n" % res )
	liwe.layman.add_js_code ( "banner2.render('%s',%s);" % ( res [ 'name' ], res [ 'tipo' ].get ( 'id' ) ) )
	return res.get ( 'html', '' )
	
"""
def load_banner ( liwe, id_tipo = 0, tipo = '' ):
	from os3.utils.utils import tmpnam

	b = Banner ( liwe )
	
	id_tipo = int ( id_tipo )
	tipo = b.get_tipo ( id_tipo = id_tipo, name = tipo )

	if not tipo:
		return "Banner type not found: %s / %s" % ( id_tipo, tipo )

	bn = b.show_banner ( id_tipo = tipo.get ( "id" ) )

	if not bn:
		return "Banner empty for type %s" % tipo

	#sys.stderr.write ( "BN: %s, tipo: %s" % ( bn, tipo ) )

	bn [ 'w' ] = tipo.get ( 'w', 0 )
	bn [ 'h' ] = tipo.get ( 'h', 0 ) 

	if bn [ 'ext' ] == 'swf':

		bn [ 'swf' ] = '/site/banner/media/%s.swf' % bn [ 'id_banner' ]
		tpl = b.templates [ 'bn-box-flash' ]

	else:
		tpl = b.templates [ 'bn-box-img' ]

	tipo [ '_banner' ] = tpl % bn
	html = b.templates [ 'bn-box' ] % tipo

	name = tmpnam ( "banner2-", ext = "div" )
	html = '<div id="%s">%s</div>' % ( name, html )

	liwe.layman.add_js_code ( "banner2.render('%s',%s);" % ( name, tipo.get ( "id" ) ) )

	return html
"""
